package com.gp.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gp.mapper.BaseUserMapper;
import com.gp.model.dto.UserAccountInfo;
import com.gp.model.entity.BaseUser;
import com.gp.service.IBaseUserService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@Service
public class BaseUserServiceImpl extends ServiceImpl<BaseUserMapper, BaseUser>
    implements IBaseUserService {

  @Autowired
  private BaseUserMapper baseUserMapper;

  @Override
  public Object getValidateUserPhone(String PHONE) {

    BaseUser baseUser = new BaseUser();
    baseUser.setPhone(PHONE);
    if (baseUserMapper.selectOne(baseUser) == null) {
      return PHONE;
    }
    return null;
  }

  @Override
  public Object getValidateUserLoginName(String LOGINNAME) {
    BaseUser baseUser = new BaseUser();
    baseUser.setLoginName(LOGINNAME);
    if (baseUserMapper.selectOne(baseUser) == null) {
      return LOGINNAME;
    }
    return null;
  }

  @Override
  public Object addUser(String PHONE, String LOGINNAME, String LOGINPASSWORD) {
    if (getValidateUserLoginName(LOGINNAME) != null) {
      BaseUser baseUser = new BaseUser(LOGINNAME, LOGINPASSWORD);
      baseUser.setPhone(PHONE);
      Integer result = baseUserMapper.insert(baseUser);
      return result;
    }
    return null;

  }

  @Override
  public boolean isExistsParent(String parentId) {
    if (!StringUtils.hasText(parentId)) {
      return false;
    }
    BaseUser user = baseUserMapper.selectById(parentId);
    return user != null;
  }

  @Override
  public Long insertUser(BaseUser user) {
    Long result = baseUserMapper.insertUser(user);
    return result;
  }

  @Override
  public UserAccountInfo selectAccountInfo(Long id) {
    UserAccountInfo info = baseUserMapper.selectAccountInfo(id);
    return info;
  }

  @Override
  public Page<BaseUser> listUser(Page<BaseUser> page) { 
    page.setRecords(baseUserMapper.listUser(page, page.getCondition()));
    return page;
  }
}
